
from appium import webdriver
from appium.webdriver.common.appiumby import AppiumBy
from appium.webdriver.common.touch_action import TouchAction
from selenium.common import NoSuchElementException
from selenium.webdriver.common.by import By

from utils.common import get_device_info
from utils.log import logger


class BasePage:
    """
    定义webdriver基础操作
    """

    # 定义测试环境地址
    HW_ENV = 'com.hanvonscanner.app:id/'
    # 导入最大图片数量
    MAX_PICTURES_NUM = 200
    # 导入最大PDF数量
    MAX_PDF_NUM = 20

    #  初始化BasePage类中的参数driver并隐式等待10秒
    def __init__(self, driver):
        self.driver = driver

    # 封装find_element中的各类元素定位方法
    def by_id(self, id):
        return self.driver.find_element(By.ID, id)

    def by_xpath(self, xpath):
        return self.driver.find_element(By.XPATH, xpath)

    def by_xpaths(self, xpath):
        return self.driver.find_elements(By.XPATH, xpath)

    def by_class_name(self, class_name):
        return self.driver.find_element(By.CLASS_NAME, class_name)

    def by_class_names(self, class_name):
        return self.driver.find_elements(By.CLASS_NAME, class_name)

    def by_uiautomator(self, uiautomator):
        return self.driver.find_element(AppiumBy.ANDROID_UIAUTOMATOR, uiautomator)

    def by_uiautomators(self, uiautomator):
        return self.driver.find_elements(AppiumBy.ANDROID_UIAUTOMATOR, uiautomator)

    def by_accessibility_id(self, accessibility_id):
        return self.driver.find_element(AppiumBy.ACCESSIBILITY_ID, accessibility_id)

    # 元素判断
    def is_element(self, element):
        source = self.driver.page_source
        if element in source:
            return True
        else:
            return False

    # 触屏操作
    def drag(self, bx=0.50, bw=0.05, by=0.4, bz=0.9, duration=800):
        x = self.driver.get_window_size()['width']
        y = self.driver.get_window_size()['height']
        sx = x * bx
        ex = x * bw
        sy = y * by
        ey = y * bz
        return self.driver.swipe(sx, sy, ex, ey, duration)  # 模拟器800







